<template>
  <div>
    <Category :scene="scene" />
    <el-card class="spu_card">
      <div v-show="sight === 0">
        <el-button
          @click="addSpu"
          type="primary"
          icon="plus"
          :disabled="!categoryStore.c3Id"
        >
          添加SPU
        </el-button>
        <el-table
          v-loading="loading"
          class="spu_table"
          :border="true"
          :data="records"
        >
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="spuName"
            label="SPU名称"
            width="250"
          ></el-table-column>
          <el-table-column
            prop="description"
            label="SPU描述"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作" width="450">
            <template #="{ row }">
              <el-button
                @click="addSku(row)"
                type="primary"
                icon="Plus"
                title="添加SKU"
              ></el-button>
              <el-button
                @click="updateSpu(row)"
                type="primary"
                icon="Edit"
                title="修改SPU"
              ></el-button>
              <el-button
                @click="findSku(row)"
                type="info"
                icon="View"
                title="查看SKU列表"
              ></el-button>
              <el-popconfirm
                @confirm="deleteSpu(row)"
                :title="`确认删除${row.spuName}?`"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    icon="Delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
          <template #empty>
            <img
              class="table_emptyImg"
              src="../../../assets/images/Minions.svg"
            />
            <p class="table_emptyP">说了人家没数据呢?</p>
          </template>
        </el-table>
        <!-- 分页器 -->
        <div class="demo-pagination-block">
          <el-pagination
            v-model:current-page="pageNo"
            v-model:page-size="pageSize"
            :page-sizes="[3, 5, 7, 9]"
            layout="prev, pager, next, jumper, ->, sizes, total"
            :total="total"
            @current-change="getHasSpu"
            @size-change="changeSize"
          />
        </div>
      </div>
      <!-- 添加或者修改SpuForm组件 -->
      <SpuForm
        ref="SpuFormDom"
        v-show="sight === 1"
        @changeSight="changeSight"
      />
      <!-- 添加或者修改SkuForm组件 -->
      <SkuForm
        ref="SkuFormDom"
        v-show="sight === 2"
        @changeSight="changeSight"
      />
    </el-card>
    <!-- dialog对话框:展示已有的SKU数据 -->
    <el-dialog v-model="dialogFormVisible" title="SKU列表">
      <el-table v-loading="loading" :border="true" :data="skuArr">
        <el-table-column prop="skuName" label="SKU名字"></el-table-column>
        <el-table-column prop="price" label="SKU价格"></el-table-column>
        <el-table-column prop="weight" label="SKU重量"></el-table-column>
        <el-table-column label="SKU图片">
          <template #="{ row }">
            <img
              class="spu_table-img"
              :src="row.skuDefaultImg"
              :alt="row.skuName"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from "vue";
// 引入分类仓库
import useCategoryStore from "@/store/modules/category";
import { reqHasSpu, reqSkuList, reqRemoveSpu } from "@/api/product/spu/index";
// 引人spuForm组件
import SpuForm from "./components/spuForm.vue";
// 引人skuForm组件
import SkuForm from "./components/skuForm.vue";
import type {
  HasSpuResponseData,
  Records,
  SpuData,
  SkuInfoData,
  SkuData,
} from "@/api/product/spu/type";
import { ElMessage } from "element-plus";
let categoryStore = useCategoryStore();
// 场景的数据
let scene = ref<boolean>(true);
// 0显示已有的SPU,1添加或修改已有的SPU,2添加SKU
let sight = ref<number>(0);
// 分页器默认页码
let pageNo = ref<number>(1);
// 每一页展示几条数据
let pageSize = ref<number>(3);
// 存储已有的SPU的数据
let records = ref<Records>([]);
// 存储已有的SPU的总个数
let total = ref<number>(0);
// 获取子组件实例SpuForm
let SpuFormDom = ref<any>();
// 获取子组件实例SkuForm
let SkuFormDom = ref<any>();
// 判断数据是否全部请求完毕
let loading = ref<boolean>(false);
//  存储全部的SKU数据
let skuArr = ref<SkuData[]>([]);
// 对话框显示与隐藏
let dialogFormVisible = ref<boolean>(false);
// 监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    // 保证有三级分类ID
    if (!categoryStore.c3Id) return;
    getHasSpu();
  },
);

// 此方法执行:可以获取某一个三级分类下全部的已有的SPU
const getHasSpu = async (pager = 1) => {
  loading.value = true;
  // 修改当前页码
  pageNo.value = pager;
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  );
  if (result.code === 200) {
    records.value = result.data.records;
    total.value = result.data.total;
  }
  loading.value = false;
};

// 分页器下拉菜单发生变化的时候触发
const changeSize = () => {
  getHasSpu();
};
// 添加新的SPU按钮回调
const addSpu = () => {
  sight.value = 1;
  scene.value = false;
  // 点击添加SPU按钮，调用子组件的方法初始化数据
  SpuFormDom.value.initAddSpu(categoryStore.c3Id);
};

// 修改已有的SPU的按钮的回调
const updateSpu = (row: SpuData) => {
  sight.value = 1;
  scene.value = false;
  // 调用子组件实例方法获取完整已有的SPU的数据
  SpuFormDom.value.initHasSpuData(row);
};

// 子组件SpuForm绑定自定义事件:目前是让子组件通知父组件切换场景为0
const changeSight = (data: number, boolean: boolean, params: boolean) => {
  sight.value = data;
  scene.value = boolean;
  // 再次获取全部的已有SPU
  if (params) {
    // 更新数据留在当前页
    getHasSpu(pageNo.value);
  } else {
    // 添加数据回到第一页
    getHasSpu();
  }
};

// 添加SKU按钮的回调
const addSku = (row: any) => {
  sight.value = 2;
  scene.value = false;
  SkuFormDom.value.initSkuData(row);
};

// 查看SKU列表的数据
const findSku = async (row: SpuData) => {
  loading.value = await true;
  let resule: SkuInfoData = await reqSkuList(row.id as number);
  if (resule.code === 200) {
    skuArr.value = resule.data;
    dialogFormVisible.value = true;
  }
  loading.value = await false;
};

// 删除已有的SKU数据
const deleteSpu = async (row: SpuData) => {
  let result: any = await reqRemoveSpu(row.id as number);
  console.log(result);

  if (result.code === 200) {
    ElMessage({
      type: "success",
      message: "删除成功？",
    });
    // 获取剩余的SPU数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1);
  } else {
    ElMessage({
      type: "error",
      message: "删除失败？",
    });
  }
};

// 路由组件销毁前，清空仓库关于分类的数据
onBeforeUnmount(() => {
  categoryStore.$reset();
});
</script>
<style scoped lang="scss">
.spu_card {
  margin: 16px 0;

  .spu_table {
    margin: 16px 0;
  }
}

.spu_table-img {
  width: 100px;
}
</style>
